
var data = {
    labels : ["1","2","3","4","5","6","7","8","9","10"],
    datasets : [
	    {
		    fillColor : "rgba(220,220,220,0.5)",
		    strokeColor : "rgba(220,220,220,1)",
		    pointColor : "rgba(220,220,220,1)",
		    pointStrokeColor : "#fff",
		    data : [65,59,90,81,56,55,40,70,70,70]
	    },
	    {
		    fillColor : "rgba(151,187,205,0.5)",
		    strokeColor : "rgba(151,187,205,1)",
		    pointColor : "rgba(151,187,205,1)",
		    pointStrokeColor : "#fff",
		    data : [28,48,40,19,96,27,120,23,34,45]
	    }
    ]
}

$(function () {

    $('tbody').get(0).scrollTop = 100000;

    $('#chat-tab a').click(function(e){
        e.preventDefault();

        $(this).tab('show');
    });

    $('#actions-tab a').click(function(e){
        e.preventDefault();
        $(this).tab('show');
    });

    // Tratar de hacer con knockout y no con jQuery... (actualizar una variable).
    $('li#li_events').click(function(e){
        game_vc.badge_events = 0;
        $('span#badge_events_node').html(game_vc.badge_events);
    });

    // Tratar de hacer con knockout y no con jQuery... (actualizar una variable).
    $('li#li_reports').click(function(e){
        game_vc.badge_reports = 0;
        $('span#badge_reports_node').html(game_vc.badge_reports);
    });

    // Tratar de hacer con knockout y no con jQuery... (actualizar una variable).
    $('li#li_chat').click(function(e){
        game_vc.badge_chats = 0;
        $('span#badge_chats_node').html(game_vc.badge_chats);
        $('#chat-lines').animate({scrollTop: $('#chat-lines').height()}, "fast");
    });

    $('select#role_selection').on('change', function(event) {
        if ( $(this).val() == 'A' ){
            var text_info = $('#login_a_text_info');
            text_info.show();
            text_info.siblings('.text_info').hide();
        } else if( $(this).val() == 'D' ){
            var text_info = $('#login_d_text_info');
            text_info.show();
            text_info.siblings('.text_info').hide();
        } else if ( $(this).val() == 'S' ){
            var text_info = $('#login_s_text_info');
            text_info.show();
            text_info.siblings('.text_info').hide();
        } else if ( $(this).val() == 'P' ){
            var text_info = $('#login_p_text_info');
            text_info.show();
            text_info.siblings('.text_info').hide();
        }
    });

    $('.btn-new-ants').popover();

    var ctx = $("#myChart").get(0).getContext("2d");
    new Chart(ctx).Line(data);

})

function select_keypress_speed_type_ant(node_this, speed_ant){

    var type_ants_speed = [6, 6, 7, 15, 12, 3]; // rellenar desde django..

    if (node_this.hasClass("ant-5")){
        if (speed_ant > type_ants_speed[5] || speed_ant == -1){
            speed_ant = type_ants_speed[5];
        }
    } else if (node_this.hasClass("ant-0")){
        if (speed_ant > type_ants_speed[0] || speed_ant == -1){
            speed_ant = type_ants_speed[0];
        }
    } else if (node_this.hasClass("ant-1")){
        if (speed_ant > type_ants_speed[1] || speed_ant == -1){
            speed_ant = type_ants_speed[1];
        }
    } else if (node_this.hasClass("ant-2")){
        if (speed_ant > type_ants_speed[2] || speed_ant == -1){
            speed_ant = type_ants_speed[2];
        }
    } else if (node_this.hasClass("ant-4")){
        if (speed_ant > type_ants_speed[4] || speed_ant == -1){
            speed_ant = type_ants_speed[4];
        }
    } else if (node_this.hasClass("ant-3")){
        if (speed_ant > type_ants_speed[3] || speed_ant == -1){
            speed_ant = type_ants_speed[3];
        }
    }

    return speed_ant;
}

function select_speed_type_ant(input_nodes, selector_ants){

    var type_ants_speed = [6, 6, 7, 15, 12, 3]; // rellenar desde django..
    var speed_ant = -1

    if (is_number_time(input_nodes.find('input.'+selector_ants+'.ant-5').val())){
        speed_ant = type_ants_speed[5];
    } else if (is_number_time(input_nodes.find('input.'+selector_ants+'.ant-0').val())) {
        speed_ant = type_ants_speed[0];
    } else if (is_number_time(input_nodes.find('input.'+selector_ants+'.ant-1').val())){
        speed_ant = type_ants_speed[1];
    } else if (is_number_time(input_nodes.find('input.'+selector_ants+'.ant-2').val())){
        speed_ant = type_ants_speed[2];
    } else if (is_number_time(input_nodes.find('input.'+selector_ants+'.ant-4').val())){
        speed_ant = type_ants_speed[4];
    } else if (is_number_time(input_nodes.find('input.'+selector_ants+'.ant-3').val())){
        speed_ant = type_ants_speed[3];
    }

    return speed_ant;
}

function show_time_type_ant(node_input, speed_ant, selector_div_time){

    base_start = game_vc.get_base_map_id(parseInt(game_vc.base_selected));
    base_finish = game_vc.get_base_map_id(parseInt(node_input.parent().parent().data('iden').split('_')[2]));
    seconds = calculate_seconds(speed_ant, base_start, base_finish);

    node_time = node_input.parent().siblings(selector_div_time);
    node_time.show();

    date = new Date(seconds * 1000);

    if (date.getHours() < 10) {
        var hours = '0' + String(date.getHours() - 1);
    } else {
        var hours = date.getHours() - 1;
    }

    if (date.getMinutes() < 10) {
        var minutes = '0' + String(date.getMinutes());
    } else {
        var minutes = date.getMinutes();
    }

    if (date.getSeconds() < 10) {
        var seconds = '0' + String(date.getSeconds());
    } else {
        var seconds = date.getSeconds();
    }

    node_time.html("en " + hours + ":" + minutes + ":" + seconds);
    
}


function show_time_type_ant2(node_input, speed_ant, selector_div_time){

    base_start = game_vc.get_base_map_id(parseInt(game_vc.base_selected));
    base_finish = game_vc.get_base_map_id(parseInt(node_input.parent().parent().parent().data('iden').split('_')[2]));
    seconds = calculate_seconds(speed_ant, base_start, base_finish);
    node_time = node_input.parent().parent().siblings(selector_div_time);
    
    date = new Date(seconds * 1000);

    if (date.getHours() < 10) {
        var hours = '0' + String(date.getHours() - 1);
    } else {
        var hours = date.getHours() - 1;
    }

    if (date.getMinutes() < 10) {
        var minutes = '0' + String(date.getMinutes());
    } else {
        var minutes = date.getMinutes();
    }

    if (date.getSeconds() < 10) {
        var seconds = '0' + String(date.getSeconds());
    } else {
        var seconds = date.getSeconds();
    }

    node_time.html("en " + hours + ":" + minutes + ":" + seconds);
    node_time.show();
}

// Teorema de Pitágoras
function calculate_seconds(type_ant_speed, base_start, base_finish){
    diff_x = base_start.x_coordinate - base_finish.x_coordinate;
    diff_y = base_start.y_coordinate - base_finish.y_coordinate;
    hypotenuse = Math.sqrt(Math.pow(diff_x, 2) + Math.pow(diff_y, 2));

    /*
        type_ant_speed -----> 60 seconds
        hypotenuse -----> X seconds
    */

    var seconds = (hypotenuse * 60) / type_ant_speed; // 60 seconds

    // Check speed bonus
    var bonus_seconds = 0
    if (game_vc.player_is_speed_bonus == true){
        bonus_seconds += seconds * 0.1;
    }

    if (game_vc.player_is_speed_bonus_base == true){
        bonus_seconds += seconds * 0.1;
    }

    return Math.round(seconds - bonus_seconds);
}

function isNumber(n) {
    //return ( !isNaN(parseFloat(n)) && isFinite(n) );
    return ( !isNaN(parseInt(n)) && isFinite(n) );
}

function isNumberPos(n) {
    //return ( !isNaN(parseFloat(n)) && isFinite(n) );
    return ( !isNaN(parseInt(n)) && isFinite(n) && n>=0);
}

function is_number_time(n){
    return ( !isNaN(parseInt(n)) && isFinite(n) && n>0 );
}


